package com.ai.bitcoin.jpa.dao;

import com.ai.bitcoin.constant.Bcc;
import com.ai.bitcoin.jpa.entity.BccIndexInfo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * @author Jason
 * @date 2018/4/11
 **/
public interface BccIndexInfoDao extends JpaRepository<BccIndexInfo, String> {
    Page<BccIndexInfo> findByBccIdAndBatchNo(String bccId, String batchNo, Pageable pageable);

    List<BccIndexInfo> findByBccIdAndBatchNo(String bccId, String batchNo);

    List<BccIndexInfo> findByBccIdAndRecordState(String bccId, String recordState);

    @Query(nativeQuery = true, value = "SELECT * FROM bcc_index_info WHERE bcc_id =?1  AND index_title = ?2 AND create_time>DATE_SUB(curdate(),INTERVAL 1 DAY) AND create_time <DATE_SUB(curdate(),INTERVAL 0 DAY)")
    List<BccIndexInfo> findByBccIdAndIndexTitleYesterday(String bccId, String indexTitle);

    @Modifying
    @Query(nativeQuery = true, value = "DELETE from bcc_index_info where batch_no <> ?1 or record_state='" + Bcc.RecordState.PUBLISH + "'")
    void deleteOldBatch(String unPublishBatch);

    @Modifying
    @Query(nativeQuery = true, value = "update bcc_index_info set record_state='" + Bcc.RecordState.PUBLISH + "' where batch_no =?1")
    void updateNewBatch2Publish(String batch);

    Page<BccIndexInfo> findByRecordState(String recordState, Pageable pageable);

    @Modifying
    @Query(nativeQuery = true, value = "DELETE from bcc_index_info where  record_state<>'" + Bcc.RecordState.PUBLISH + "'")
    void deleteNoPublish();

    List<BccIndexInfo> findByBatchNo(String batchNo);

    List<BccIndexInfo> findByBatchNoAndIndexTitle(String batchNo, String indexTitle);

}
